package com.ruoyi.web.controller;

import com.ruoyi.system.domain.User;
import com.ruoyi.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.time.LocalDateTime;

@RestController
public class HelloController {
    @Autowired
    private UserService userService;
    private User user = new User();
    private int index = 0;
    private List list = new ArrayList();

    @GetMapping("/hello")
    public List<User> hello()
    {
        return userService.selectAllUser();
    }

    @PostMapping("/hello")
    public List helloPost(String name,int age,String sex) {
        list.clear();
        user.setName(name);
        user.setAge(age);
        user.setSex(sex);
        user.setCreateTime(LocalDateTime.now().toString());
        index++;
        userService.insertUser(user);
        list.add("已添加第"+index+"个用户");
        list.add(user);
        return list;
    }

    @PutMapping("/hello")
    public User helloPut(String name,int age,String sex) {
        user.setName(name);
        user.setAge(age);
        user.setSex(sex);
        user.setCreateTime(LocalDateTime.now().toString());
        userService.updateUser(user);
        return user;
    }

    @DeleteMapping("/hello/{name}")
    public String helloDel(@PathVariable String name) {
        userService.deleteUserByName(name);
        return "del name: "+name;
    }
}
